package com.far.game.xiuzhen.conf;



import com.far.game.xiuzhen.component.self.ParamsHandlerMethodArgumentResolver;
import com.far.game.xiuzhen.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;


@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        //super.addArgumentResolvers(argumentResolvers);
        resolvers.add(new ParamsHandlerMethodArgumentResolver());
    }

    /**
     * 注册自定义拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //可以多个拦截器
//       InterceptorRegistration interceptorDemoRegistration = registry.addInterceptor(new InterceptorDemo());
        //interceptorDemoRegistration.addPathPatterns("/..");
        //interceptorDemoRegistration.addPathPatterns("/..");

        InterceptorRegistration interceptorRegistration = registry.addInterceptor(tokenInterceptor);

        //interceptorRegistration.addPathPatterns("/count");

        //可以设置多个路径拦截
        interceptorRegistration.addPathPatterns("/**"); //拦截全部

        //设置放行
        interceptorRegistration.excludePathPatterns("/admins/login")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
        interceptorRegistration.excludePathPatterns("/doc*");


    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}